package org.hbnu.xiaomi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.hbnu.xiaomi.entity.Carts;

import java.util.List;

@Mapper
public interface CartsMapper{

    // 插入新的购物车条目
    @Insert("INSERT INTO carts (user_id, good_id, num, allprice, ordertime) VALUES (#{user_id}, #{good_id}, #{num}, #{allprice},#{ordertime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Carts cart);

    // 更新购物车
    @Update("UPDATE carts SET user_id = #{user_id} ,good_id = #{good_id}, num = #{num}, allprice = #{allprice}, ordertime = #{ordertime}  WHERE id = #{id}")
    public void update(Carts carts);


    // 删除购物车中的商品
    @Delete("DELETE FROM carts WHERE id = #{id}")
    void deleteById(int id);

    // 获取指定用户的购物车信息
    @Select("SELECT *  FROM carts WHERE user_id = #{user_id}")
    @Results({
            //java->mysql
            @Result(property = "good_id", column = "good_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "user_id", column = "user_id", jdbcType = JdbcType.INTEGER),

    })
    List<Carts> getByUsersId(@Param("user_id")int user_id);

    //获取指定编号的购物车信息
    @Select("select * from carts where id=#{id}")
    Carts getById(@Param("id") int id);

    // 新增方法，用于删除特定用户和商品的购物车记录
    @Delete("DELETE FROM carts WHERE user_id = #{user_id} AND good_id = #{good_id}")
    void deleteByUserIdAndGoodId(Integer user_id, Integer good_id);

    // 新增方法，用于查找相同good_id和user_id的记录的num值
    @Select("SELECT num FROM carts WHERE good_id = #{good_id} AND user_id = #{user_id}")
    Integer getNumByGoodIdAndUserId(@Param("good_id") Integer good_id, @Param("user_id") Integer user_id);
}
